package com.kum.controller;

import com.kum.annotations.UserId;
import com.kum.domain.R;
import com.kum.domain.entity.SysUser;
import com.kum.service.SysUserService;
import com.kum.service.UserCacheService;
import com.kum.utils.IpUtils;
import com.kum.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @version V1.0
 * @Package com.kum.controller
 * @auhter 枯木Kum
 * @date 2021/6/16-10:03 AM
 * <p>...</p>
 */

@RestController
@RequestMapping("/user")
public class SysUserController {

    @Autowired
    private SysUserService userService;
    @Autowired
    private UserCacheService userCacheService;

    @PostMapping("/login")
    public R login(@RequestBody SysUser user) {
        String ip = IpUtils.getIpAddr();
        SysUser result = userService.login(user);
        if (result == null) {
            return R.fail("用户名或密码错误");
        }
        result.setHostAddress(ip);
        String token = JwtUtil.sign(result);
        //缓存用户脚本信息
        userCacheService.putUserScriptMatchs(ip,result.getId());
        return R.ok(token);

    }

    @GetMapping("/checkToken")
    public R checkToken(@UserId String userId){
        if(userId == null){
            return R.fail();
        }
        return R.ok();
    }

}
